package cs.android.lang;

import cs.android.ActivityView;
import cs.java.event.Event;
import cs.java.event.Event.EventRegistration;
import cs.java.event.Job;

public abstract class RequestJob<Request extends ServerRequest> extends Job<Request> {

	private final ActivityView parent;
	protected final Request request;

	//	public RequestJob(IsActivityView parent, Event<Request> event) {
	//		this.parent = parent;
	//		event.add(this);
	//	}

	public RequestJob(ActivityView parent, Request request) {
		this.parent = parent;
		this.request = request;
		request.getOnSuccess().add(this);
		request.getOnLoaded().add(this);
		request.getOnFailed().add(this);
		request.getOnDone().add(this);
	}

	protected boolean isDone() {
		return event.equals(request.getOnDone());
	}

	protected boolean isFailed() {
		return event.equals(request.getOnFailed());
	}

	protected boolean isLoaded() {
		return event.equals(request.getOnLoaded());
	}

	protected boolean isNotSuccess() {
		return isDone() && !request.isSuccess();
	}

	protected boolean isSuccess() {
		return event.equals(request.getOnSuccess());
	}

	@Override public <T> void onEvent(Event<T> event, EventRegistration registration, T argument) {
		if (!parent.isPaused()) super.onEvent(event, registration, argument);
	}
}